package com.diyunapp.xilinapp.me;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.RechargeRecordBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.AbDateUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RechargeRecordActivity extends StatusBarFragmentActivity {

    @Bind(R.id.money_recorder_list)
    ListView moneyRecorderList;
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.notice_refresh_view)
    SwipeRefreshLayout noticeRefreshView;

    private List<RechargeRecordBean.DateBean> info = new ArrayList<>();
    private LayoutInflater inflater;
    private RecorderAdapter recorderAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_recharge_record);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        recorderAdapter=new RecorderAdapter(this);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.rere_menubar_inc);
        super.MenuWidget(headView);
        getData();
        moneyRecorderList.setAdapter(recorderAdapter);
        noticeRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        noticeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                getData();
            }
        });


    }

    @OnClick(R.id.go_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
        }
    }

    private void getData() {
        String url = ""+ MainActivity.url+"act=goods&op=recharge_record&username="+MainActivity.myLogin.getUsername()+"";

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        noticeRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                info.clear();
                                JSONArray listArray = resultObject.getJSONArray("date");
                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.getJSONObject(i);
                                    long pdr_add_time = recordObject.optLong("pdr_add_time");
                                    String pdr_amount = recordObject.optString("pdr_amount");
                                    String pdr_payment_state = recordObject.getString("pdr_payment_state");
                                    String pdr_sn = recordObject.getString("pdr_sn");
                                    String data=pdr_add_time+"=="+pdr_amount+"=="+pdr_payment_state+"=="+pdr_sn;
                                    Log.i("==============", "data==========" + data);
                                    info.add(new RechargeRecordBean.DateBean(pdr_add_time, pdr_amount, pdr_payment_state,
                                            pdr_sn));
                                }
                                recorderAdapter.setList(info);
                                recorderAdapter.notifyDataSetChanged();

                            } else {
                                toast("当前暂无数据");
                                noticeRefreshView.setVisibility(View.GONE);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            noticeRefreshView.setVisibility(View.GONE);
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
//                        bidFreshLv.onRefreshComplete();
                        noticeRefreshView.setRefreshing(false);
                        toast("请求失败");
                        noticeRefreshView.setVisibility(View.GONE);
                    }
                });
    }

    class RecorderAdapter extends BaseAdapter {
        private List<RechargeRecordBean.DateBean> info = new ArrayList<RechargeRecordBean.DateBean>();
        private Context context;
        public RecorderAdapter(Context context) {
            super();
            this.context = context;
        }

        public void setList(List<RechargeRecordBean.DateBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private ViewHolder viewHolder;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (null == convertView) {

                convertView = inflater.inflate(R.layout.item_rexcharge_recorder, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            RechargeRecordBean.DateBean data_info = info.get(position);
            viewHolder.rechargeTime.setText(AbDateUtil.getStringByFormat(data_info.getPdr_add_time()*1000,"yyyy-MM-dd"));
            viewHolder.tradeDanhao.setText(data_info.getPdr_sn());
            viewHolder.rechargeMoney.setText(data_info.getPdr_amount());
            String state=data_info.getPdr_payment_state();
            if (state=="1"||state.equals("1")){
            viewHolder.rechargeState.setText("已支付");
            }else if(state=="0"||state.equals("0")){
                viewHolder.rechargeState.setText("未支付");
            }
            return convertView;

        }

        class ViewHolder {
            @Bind(R.id.trade_danhao)
            TextView tradeDanhao;
            @Bind(R.id.recharge_time)
            TextView rechargeTime;
            @Bind(R.id.recharge_money)
            TextView rechargeMoney;
            @Bind(R.id.recharge_state)
            TextView rechargeState;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
